package com.xjh.him.fragment.emotion.classic;

import android.os.Build;
import android.util.ArrayMap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.xjh.him.adapter.emotion.classic.ClassicEmotionAdapter;
import com.xjh.him.adapter.entity.ChatMessage;
import com.xjh.him.activity.chat.ChatActivity;
import com.xjh.him.fragment.emotion.BaseEmotionPanelFragment;
import com.xjh.him.utils.EmotionUtils;
import com.xjh.him.utils.Utils;
import com.xjh.him.utils.XToastUtils;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.utils.DensityUtils;

import java.util.ArrayList;

@Page(name = "经典表情面板")
public class ClassicEmotionPanelFragment extends BaseEmotionPanelFragment {
    private ChatActivity chatActivity;

    public ClassicEmotionPanelFragment(ChatActivity chatActivity) {
        this.chatActivity = chatActivity;
    }

    @Override
    protected void initViews() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            ArrayMap<String, Integer> classicEmojiMap = EmotionUtils.getEmojiMap(EmotionUtils.EMOTION_CLASSIC_TYPE);
            int rowNum = (int)Math.ceil(classicEmojiMap.size() / EmotionUtils.DEFAULT_COLUMN_NUM);
            int[] args = EmotionUtils.getGridArgs(getActivity(), rowNum);
            int itemWidth = args[0],
                    gvHeight = args[1],
                    gvWidth = DensityUtils.getDisplayMetrics().widthPixels,
                    padding = EmotionUtils.getEmotionPadding(getActivity());

            GridView gv = super.initGridViewParams(padding, gvWidth, gvHeight);
            ArrayList<String> emotionKeys = new ArrayList<>(classicEmojiMap.keySet());
            ClassicEmotionAdapter adapter = new ClassicEmotionAdapter(
                    getActivity(),
                    emotionKeys,
                    itemWidth);
            gv.setAdapter(adapter);
            gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String key = emotionKeys.get(position);
                    Integer resId = classicEmojiMap.get(key);
                    chatActivity.mAdapter.listData().add(new ChatMessage(
                            ChatMessage.DIRECTION_RIGHT, ChatMessage.TYPE_IMAGE,
                            "李四", String.valueOf(resId),
                            Utils.currentDateTime()
                    ));
                    chatActivity.mAdapter.notifyItemInserted(chatActivity.mAdapter.getItemCount()-1);
                    chatActivity.recyclerScrollToBottom();
                }
            });
        }else {
            XToastUtils.toast("build sdk version must greater 19, current is " + Build.VERSION.SDK_INT);
        }
    }
}
